package adapt

/**
1、获取个人信息，角色id，部门id
2、根据角色获取授权列表
3、根据部门id，获取
4、根据用户id，获取授权列表
*/

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go-vue/admin/conf"
	"go-vue/admin/model"
	"strings"
)

type UserInfo struct {
	UserId     int64
	RoleId     int64
	DepartId   int64
	UserName   string
	Mobile     string
	RealName   string
	State      conf.State
	Err        error
	Permission []string
	queryIds   []int64
}

// ComparePermission 对比是否拥有权限
func ComparePermission(ctx *gin.Context, name string) bool {
	userInfo, ok := ctx.Get("UserInfo")
	if ok == false {
		return true
	}
	Info := userInfo.(*UserInfo)
	//fmt.Printf(" %+v,userInfo: %+v\n",reflect.TypeOf(Info),Info)
	if len(Info.Permission) == 0 {
		return true
	}
	for _, value := range Info.Permission {
		if value == name {
			return false
		}
	}
	return true
}

// GetPermissionMenu 获取权限列表
func GetPermissionMenu(UserId int64) *UserInfo {
	auth := &UserInfo{
		UserId: UserId,
	}
	auth.getUserInfo()
	if auth.Err != nil {
		return auth
	}

	auth.GetPermissionList()
	return auth
}

// 获取用户信息
func (auth *UserInfo) getUserInfo() *UserInfo {
	info := &model.UserMember{
		Uid: auth.UserId,
	}
	info.One()
	if info.State != conf.StateOn {
		auth.Err = errors.New("用户不存在")
		return auth
	}

	auth.RoleId = info.RoleId
	auth.DepartId = info.DepartId
	auth.RealName = info.RealName
	auth.Mobile = info.Mobile
	auth.State = info.State

	return auth
}

// GetPermissionList 权限列表获取
func (auth *UserInfo) GetPermissionList() *UserInfo {
	user := (&model.SystemPermission{
		Type:       model.PermissionPerson,
		RelationId: auth.UserId,
	}).One()
	if user != nil {
		auth.Permission = append(auth.Permission, strings.Split(user.Permissions, ",")...)
		return auth
	}

	role := (&model.SystemPermission{
		Type:       model.PermissionRole,
		RelationId: auth.RoleId,
	}).One()
	if role != nil {
		auth.Permission = append(auth.Permission, strings.Split(role.Permissions, ",")...)
		return auth
	}
	return auth
}

// GetMyMenu 权限列表获取
func (auth *UserInfo) GetMyMenu() []*model.SystemMenuList {
	//查询菜单id
	idsApi := new(model.SystemMenuApiList).GetAllMenuIds(auth.Permission)
	ids := new(model.SystemMenu).GetAllMenuIds(auth.Permission)
	if len(idsApi) > 0 {
		ids = append(ids, idsApi...)
	}

	var (
		menuList []model.SystemMenu
		//menuList model.SystemMenuSort
	)

	for _, id := range ids {
		tmpMenuList := auth.myMenu(id, nil)
		menuList = append(menuList, tmpMenuList...)
	}
	//sort.Sort(menuList)
	var readIds []int64
	for _, item := range menuList {
		readIds = append(readIds, item.Id)
	}

	return new(model.SystemMenu).GetMyMenu(readIds)
}

//递归查询所有菜单
func (auth *UserInfo) myMenu(id int64, menuList []model.SystemMenu) []model.SystemMenu {
	//已经处理过的不在查询
	for _, v := range auth.queryIds {
		if v == id {
			return menuList
		}
	}

	auth.queryIds = append(auth.queryIds, id)
	info := (&model.SystemMenu{Id: id}).One()
	if info == nil || info.State == conf.StateOff {
		return menuList
	}
	menuList = append(menuList, *info)
	if info.Pid != 0 {
		return auth.myMenu(info.Pid, menuList)
	}
	return menuList
}
